package com.lagou.homework.part02

import scala.io.StdIn
import scala.util.Random

/**
 * @author: yehw
 * @date: 2020/9/29 23:18
 * @description: 猜拳游戏
 */
object Part02 {
  //对手名字
  val roles = Map((1 -> "刘备"), (2 -> "关羽"), (3 -> "张飞"))
  //石头剪刀布
  val mora = Map((1 -> "剪刀"), (2 -> "石头"), (3 -> "布"))
  //我方积分
  var iPutPoints = 0
  //对方积分
  var opponentPoints = 0

  def main(args: Array[String]): Unit = {
    println("************❤❤❤❤❤❤❤❤❤❤******************")
    println("************开始猜拳游戏******************")
    println("************❤❤❤❤❤❤❤❤❤❤******************")
    println("************请输入对手名字*****************")
    println(s"${roles}")
    val role = StdIn.readLine().toInt
    role match {
      case 1 => println(s"你的对手是${roles.get(role)}")
      case 2 => println(s"你的对手是${roles.get(role)}")
      case 3 => println(s"你的对手是${roles.get(role)}")
      case _ => println("参数错误")
    }
    println("准备好了吗（y/n）")
    var isRead: String = StdIn.readLine()
    var isReadFlag: Boolean = true
    //转大写后比较
    isRead.toUpperCase match {
      case "Y" => isReadFlag = true
        println("开始游戏")
      case "N" => isReadFlag = false
        System.exit(1)
    }
    while (isReadFlag) {
      println(s"请出拳$mora")
      //你输入的数字
      val moraByU: Int = StdIn.readLine().toInt
      val random = new Random()
      val inclusive = 1 to 3
      //随机生成一个对手数字
      val moraByM = inclusive(random.nextInt(inclusive.length))
      //获得你出的对象
      val option = mora.get(moraByU)
      val option2 = mora.get(moraByM)
      option match {
        case Some(x) => println(s"你出的是$x")
        case None => println("出得不符合规则")
          isReadFlag = false
          System.exit(1)
      }
      option2 match {
        case Some(x) => println(s"${roles.get(role)}出的是$x")
        case None => println("出得不符合规则")
          isReadFlag = false
          System.exit(1)
      }
      if (moraByU - moraByM == 1 || moraByU - moraByM == (-2)) {
        iPutPoints += 2
        println("结果：你赢")
      } else if (moraByU - moraByM == 0) {
        iPutPoints += 1
        opponentPoints += 1
        println("结果：和局")
      } else {
        opponentPoints += 3
        println(s"结果：${roles.get(role)}赢")
      }
      println("继续开始游戏吗？")
      isRead = StdIn.readLine()
      isRead.toUpperCase match {
        case "Y" => isReadFlag = true
        case "N" => isReadFlag = false
          if(iPutPoints>opponentPoints){
            println(s"最终对战你赢!$opponentPoints")
          }else if(iPutPoints==opponentPoints){
             println(s"对战和局,积分是$iPutPoints")
          }else{
            println(s"最终${roles.get(role)}赢!积分是：$opponentPoints")
          }
          System.exit(1)
      }
    }
  }

}
